package com.example.tang.zhayan.ui;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.tang.zhayan.R;
import com.example.tang.zhayan.fragment.UserCardFragment;
import com.example.tang.zhayan.fragment.UserFocusFragment;
import com.example.tang.zhayan.fragment.UserGroupFragment;
import com.squareup.picasso.Picasso;

import org.w3c.dom.Text;
import org.xutils.image.ImageOptions;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户界面
 * Created by tang on 2015/12/18.
 */
@ContentView(R.layout.activity_user)
public class UserActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener {
    private String icon = "";
    private String userName = "";
    @ViewInject(R.id.iv_user_image)
    private ImageView imgHead;
    @ViewInject(R.id.tv_user_name)
    private TextView textName;
    @ViewInject(R.id.ll_user_container)
    private LinearLayout userContainer;
    @ViewInject(R.id.rg_user_selected)
    private RadioGroup radioGroup;
    @ViewInject(R.id.iv_user_back)
    private ImageView imgBack;
    @ViewInject(R.id.iv_user_setting)
    private ImageView imgSetting;
    private SharedPreferences preferences;

    private List<Fragment> list;
    private FragmentManager manger;
    private FragmentTransaction transaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        preferences = getSharedPreferences("userMsg", Context.MODE_PRIVATE);
        list = new ArrayList<>();
        manger = getSupportFragmentManager();
        transaction = manger.beginTransaction();
        getFragmentData();

        //获取用户信息
        Intent intent = getIntent();
        //显示用户头像和名字
        icon = intent.getStringExtra("icon");
        userName = intent.getStringExtra("userName");
        showUserMsg();

        transaction.add(R.id.ll_user_container, list.get(0));
        transaction.commit();

        //返回的点击事件
        imgBack.setOnClickListener(this);
        imgSetting.setOnClickListener(this);
        //radiogroup的点击事件
        radioGroup.setOnCheckedChangeListener(this);
        RadioButton radioButton = (RadioButton) radioGroup.getChildAt(0);
        radioButton.setChecked(true);
    }

    /**
     * 显示用户的信息
     */
    private void showUserMsg() {
        //点击个人穿过来的数据
        if (!icon.equals("") && !userName.equals("")) {
            ImageOptions options = new ImageOptions.Builder()
                    .setImageScaleType(ImageView.ScaleType.FIT_XY)
                    .setCircular(true)
                    .build();
            x.image().bind(imgHead, icon, options);
            textName.setText(userName);
        } else {
            //直接从本地读取的数据
            ImageOptions options = new ImageOptions.Builder()
                    .setImageScaleType(ImageView.ScaleType.FIT_XY)
                    .setCircular(true)
                    .setFailureDrawable(getResources().getDrawable(R.mipmap.default_loader_image1))
                    .build();
            x.image().bind(imgHead, preferences.getString("icon", ""), options);
            textName.setText(preferences.getString("userName", "没有用户登录~"));
        }
    }

    /**
     * 显示详情的fragment
     *
     * @param index
     */
    private void showFragment(int index) {
        transaction = manger.beginTransaction();
        Fragment fragment = null;
        for (int i = 0; i < list.size(); i++) {
            fragment = list.get(i);
            if (!fragment.isAdded()) {
                transaction.add(R.id.ll_user_container, fragment);
            }
        }
        for (int i = 0; i < list.size(); i++) {
            fragment = list.get(i);
            if (i == index) {
                transaction.show(fragment);
            } else {
                transaction.hide(fragment);
            }
        }
        transaction.commit();
    }

    private void getFragmentData() {
        list.add(UserCardFragment.newInstance());
        list.add(UserFocusFragment.newInstance());
        list.add(UserGroupFragment.newInstance());
    }

    /**
     * 选项点击事件
     *
     * @param group
     * @param checkedId
     */
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rb_user_card:
                showFragment(0);
                break;
            case R.id.rb_user_focus:
                showFragment(1);
                break;
            case R.id.rb_user_group:
                showFragment(2);
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_user_back:
                this.finish();
                break;
            case R.id.iv_user_setting:
                Intent intent = new Intent(this, SettingActivity.class);
                startActivity(intent);
                break;
        }
    }
}
